package com.cyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.DoctorDao;
import com.cyz.dao.OldDao;
import com.cyz.dao.UserDao;
import com.cyz.entity.Doctor;
import com.cyz.entity.Old;
import com.cyz.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DoctorDao doctorDao;

    @Autowired
    private OldDao oldDao;

    @PostMapping("/sign")
    public String sign(String name, String account, String pwd, String role){
        User entity = userDao.selectOne(new QueryWrapper<User>().eq("name", name));
        if (entity != null){
            return "该用户已经存在";
        }
        User newUser = new User();
        newUser.setName(name);
        newUser.setAccount(account);
        newUser.setPwd(pwd);
        newUser.setRole(role);
        if (role.equals("医生")){
            Doctor doctor = new Doctor();
            doctor.setName(name);
            doctorDao.insert(doctor);
        }
        if (role.equals("老人")){
            Old old = new Old();
            old.setName(name);
            oldDao.insert(old);
        }
        int insert = userDao.insert(newUser);
        return insert == 0 ? "添加失败" : "添加成功";
    }

    @PostMapping("/login")
    public String login(String account, String pwd, String role){
        User entity = userDao.selectOne(new QueryWrapper<User>().eq("account", account)
                .eq("pwd", pwd)
                .eq("role", role));
        return entity != null ? "登录成功" : "登录失败";
    }
}
